package com.wechatapp.mengyao.poetry.controller;

import com.wechatapp.mengyao.poetry.biz.DailyPoetryBiz;
import com.wechatapp.mengyao.poetry.biz.PoetryBiz;
import com.wechatapp.mengyao.poetry.core.JsonFieldFilterDef;
import com.wechatapp.mengyao.poetry.core.PageResult;
import com.wechatapp.mengyao.poetry.core.RestResponse;
import com.wechatapp.mengyao.poetry.vo.ChinesePoetryView;
import com.wechatapp.mengyao.poetry.vo.ChinesePoetryPageView;
import com.wechatapp.mengyao.poetry.entity.PoetryType;
import com.wechatapp.mengyao.poetry.service.ChinesePoetryService;
import com.wechatapp.mengyao.poetry.vo.DailyPoetryView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author yanghuaxu
 * @date 2025/5/11 20:43
 */
@RestController
@RequestMapping("/api/v1/poetry")
public class PoetryController {

    @Autowired
    private ChinesePoetryService chinesePoetryService;
    @Autowired
    private PoetryBiz poetryBiz;
    @Autowired
    private DailyPoetryBiz dailyPoetryBiz;

    @GetMapping("/{id}")
    @JsonFieldFilterDef({
            @JsonFieldFilterDef.Entry(type = ChinesePoetryView.class, exclude = {"contentString", "tagString"})
    })
    public RestResponse<ChinesePoetryView> getById(@PathVariable Integer id) {

        return RestResponse.ok(poetryBiz.getPoetryDetail(id));
    }

    @GetMapping("/page")
    @JsonFieldFilterDef({
            @JsonFieldFilterDef.Entry(type = ChinesePoetryPageView.class, exclude = {"contentString"})
    })
    public RestResponse<PageResult<ChinesePoetryPageView>> getByPage(@RequestParam Map<String, String> map) {

        return RestResponse.ok(chinesePoetryService.listByPage(map));
    }

    @GetMapping("/es/page")
    @JsonFieldFilterDef({
            @JsonFieldFilterDef.Entry(type = ChinesePoetryPageView.class, exclude = {"contentString"})
    })
    public RestResponse<PageResult<ChinesePoetryPageView>> getByPageFromEs(@RequestParam Map<String, String> map) {

        PageResult<ChinesePoetryPageView> result = null;
        try {
            result = chinesePoetryService.listByPageFromEs(map);
        } catch (Exception e) {
            e.printStackTrace();
            // 如果es查询出错则用数据库查询
            result = chinesePoetryService.listByPage(map);
        }
        return RestResponse.ok(result);
    }

    @GetMapping("/random")
    @JsonFieldFilterDef({
            @JsonFieldFilterDef.Entry(type = ChinesePoetryView.class, exclude = {"contentString", "tagString"})
    })
    public RestResponse<ChinesePoetryView> getRandomById(@RequestParam Integer currentId) {

        return RestResponse.ok(chinesePoetryService.getRandomById(currentId));
    }

    @GetMapping("/type")
    public RestResponse<List<PoetryType>> getAllPoetryType() {

        return RestResponse.ok(chinesePoetryService.getAllPoetryType());
    }
    @GetMapping("/everyday")
    public RestResponse<DailyPoetryView> getTodayPoetry() {

        return RestResponse.ok(dailyPoetryBiz.getTodayPoetry());
    }


}
